<?php
namespace app\farm\model;

use app\BaseModel;
use longbingcore\wxcore\YsCloudApi;
use think\facade\Db;

class Monitor extends BaseModel
{
    //定义表名
    protected $name = 'lbfarm_monitor';


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();
        
        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page){

        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }



    /**
     * @author chenniang
     * @DataTime: 2021-12-14 16:07
     * @功能说明:
     */
    public function eventClaim($id,$data){

        $this->where(['obj_id'=>$id,'type'=>2])->delete();

        if(!empty($data['monitor'])){

            foreach ($data['monitor'] as $k => $v){

                $insert[$k] = [

                    'uniacid' => $data['uniacid'],

                    'obj_id'  => $id,

                    'monitor_id'=> $v,

                    'type'    => 2
                ];

            }

            $this->saveAll($insert);

        }

        return true;

    }


    /**
     * @author chenniang
     * @DataTime: 2022-04-11 14:28
     * @功能说明:获取监控地址
     */
    public function getVideoUrl($id){

        $dis = [

            'id' => $id
        ];

        $data = $this->dataInfo($dis);

        if(empty($data)){

            return [];
        }

        $this->dataUpdate($dis,['iv'=>$data['iv']+1]);

        $api = new YsCloudApi($data['uniacid']);

        $url = $api->getVideoInfo($data['deviceSerial'],$data['channelNo']);

        return $url;

    }


    /**
     * @author chenniang
     * @DataTime: 2022-04-11 15:57
     * @功能说明:农场监控列表
     */
    public function farmerMonitorList($dis,$page=10){

        $data = $this->alias('a')
                ->join('lbfarm_farmer b','a.farmer_id = b.id')
                ->where($dis)
                ->field('a.*,b.address')
                ->group('a.id')
                ->order('a.id desc')
                ->paginate($page)
                ->toArray();

        return $data;

    }








}